package ar.edu.isft189.utility;

import java.sql.Connection;
import java.sql.SQLException;

public class DBUtility {
	private static Connection c = null;
	private static org.firebirdsql.pool.FBWrappingDataSource dataSource = new org.firebirdsql.pool.FBWrappingDataSource();
	private static String filesep = System.getProperty("file.separator");
	private static String databaseURL = "localhost/3050:C:" + filesep + "SGANS"
			+ filesep + "isft.fdb";
	private static String dbDescription = "An Example Database";
	private static String user = "sysdba";
	private static String password = "masterkey";
	private static String rol = "sysdb";
	private static int loginTimeOut = 10; // 10 segundos de time-out para login
	private static java.sql.DatabaseMetaData dbMetaData = null;

	public static Connection getConnection() {
		if (c != null)
			return c;
		else {
			try {
				if (c == null) {
	                System.out.println("Me conecto!");
	                // Set the standard properties
	                dataSource.setDatabase(databaseURL);
	                dataSource.setDescription(dbDescription);
	                dataSource.setRoleName(rol);
	                dataSource.setLoginTimeout(loginTimeOut);
	                c = dataSource.getConnection(user, password);

	                System.out.println("Connection established.");
	                c.setAutoCommit(false);
	                System.out.println("Auto-commit is disabled.");
	                dbMetaData = c.getMetaData();
	                // Ok, let's query a driver/database capability
	                if (dbMetaData.supportsTransactions()) {
	                    System.out.println("Transactions are supported.");
	                } else {
	                    System.out.println("Transactions are not supported.");
	                }
	            } else {
	                System.out.println("JsonMesaExamenServlet: ya estaba conectado!");
	            }
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			return c;
		}

	}

}
